/*
 *
 * MySQLDAL.java
 * Copyright(c) 2013 UcentralSoft, Todos los derechos reservados
 */
package controlador;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Administra la conexion a la BD de MySQLDAL
 *
 * @version 1.0 [2013/10/13]
 * @author Jorge Barrera
 */
public final class MySQLDAL implements IDAL {

    private static MySQLDAL instance = null;
    // database connection settings
    private static String dbServer = "jdbc:mysql://localhost:3306/ucentralsoft";
    private static String dbUser = "root";
    private static String dbPass = "123456789";
    private static Connection connection = null;
    private static ResultSet resultSet = null;

    /**
     * Constructor de la clase
     *
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private MySQLDAL() throws SQLException, ClassNotFoundException {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        connection = DriverManager.getConnection(dbServer, dbUser, dbPass);
        setData("SET GLOBAL max_allowed_packet = 104857600;");
    }

    /**
     * Pemite obtener la instancia de la clase
     *
     * @return MySQLDAL
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static MySQLDAL getInstance() throws SQLException,
            ClassNotFoundException {
        if (instance == null || connection.isClosed()) {
            instance = new MySQLDAL();
        }
        return instance;
    }

    /**
     * Cierra la conexion a la bd
     *
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    @Override
    public void closeConnect() throws ClassNotFoundException, SQLException {
        if (connection != null) {
            connection.close();
        }
    }

    /**
     * Pemite obtener set de datos de una consulta
     *
     * @param query
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Override
    public ResultSet getData(String query) throws SQLException,
            ClassNotFoundException {
        Statement statement = connection.createStatement();
        resultSet = statement.executeQuery(query);
        return resultSet;
    }

    /**
     * Permite ejecutar acciones de cambio de datos
     *
     * @param query
     * @return boolean
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Override
    public boolean setData(String query) throws SQLException,
            ClassNotFoundException {
        Statement statement = connection.createStatement();
        return statement.executeUpdate(query) > 0;
    }

    /**
     * Permite obtener el resultado de un query
     *
     * @return ResultSet
     */
    @Override
    public ResultSet getResultSet() {
        return resultSet;
    }
}
